package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.OrderDetails;
import com.itheima.entity.Orders;
import com.itheima.jsonresult.JsonResult;
import com.itheima.mapper.OrdersMapper;
import com.itheima.service.OrderDetailsService;
import com.itheima.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author linying
 * @since 2022-10-13
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrderDetailsService orderDetailsService;

    /**
     * 修改订单状态
     * @param
     * @param
     * @return
     */
    @Override
    public JsonResult status(Orders orders) {
        // 添加查询的条件id
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id", orders.getId());

        //修改订单状态
        if (orders.getStatus()==0){
            orders.setStatus(1);
            update(orders,wrapper);
            return new JsonResult(1,null,null,null);
        }else {
            orders.setStatus(0);
            update(orders,wrapper);
            return new JsonResult(1,null,null,null);
        }
    }

    /**
     * 订单分页查询
     * @param beginTime
     * @param endTime
     * @param number
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult getPage(String beginTime, String endTime, String number, Integer page, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        // 订单号可选非空条件判断
        if (!StrUtil.isBlank(number)) {
            wrapper.eq("number", number);
        }
        // 开始时间可选非空条件判断
        if (!StrUtil.isBlank(number)) {
            wrapper.eq("order_time", beginTime);
        }
        // 结束时间可选非空条件判断
        if (!StrUtil.isBlank(number)) {
            wrapper.eq("checkout_time", endTime);
        }

        // 调用分页查询方法
        Page jsonpage = (Page) page(new Page<>(page, pageSize), wrapper);

        return JsonResult.success(jsonpage);
    }

    /**
     *提交订单
     * @param orders
     * @return
     */
    @Override
    public JsonResult submit(Orders orders) {
        //添加订单
        save(orders);

        return JsonResult.success("提交成功");
    }

    /**
     * 订单列表
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public JsonResult userPage(Integer page, Integer pageSize) {
        // 调用分页查询方法
        Page<Orders> jsonpage = page(new Page<>(page, pageSize));
        //获取订单详情集合
        List<Orders> records = jsonpage.getRecords();
        //遍历设置
        for (Orders record : records) {
            record.setOrderDetails(orderDetailsService.list((Wrapper<OrderDetails>) new QueryWrapper().eq("order_id",record.getId())));
        }

        return JsonResult.success(jsonpage);
    }
}
